package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.bean.SetSecurityBeanTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SetSecurityActivityTing extends AppCompatActivity {

    private EditText editNameTing;
    private Button btnMibaoTing;
    private ImageView ivBack2Ting;
    private TextView titleBarNameTing;
    private ImageView ivXiaokeaiTing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_security_ting);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initView();
    }

    private void initView() {
        ivBack2Ting = (ImageView) findViewById(R.id.iv_back2_ting);
        titleBarNameTing = (TextView) findViewById(R.id.title_bar_name_ting);
        ivXiaokeaiTing = (ImageView) findViewById(R.id.iv_xiaokeai_ting);
        editNameTing = (EditText) findViewById(R.id.edit_name_ting);
        btnMibaoTing = (Button) findViewById(R.id.btn_mibao_ting);
        titleBarNameTing.setText("设置密保");
        ivXiaokeaiTing.setBackgroundResource(R.drawable.boy);
        ivBack2Ting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetSecurityActivityTing.this.finish();
            }
        });
        btnMibaoTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_click();
            }
        });
    }

    private void btn_click() {
        String validateName = editNameTing.getText().toString().trim();//获取用户输入的姓名
        if (TextUtils.isEmpty(validateName)) {
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("WARNING")
                    .setMessage("请输入要验证的姓名")
                    .setDuration(1500)
                    .show();
            return;
        } else {
            JsonObject jo = new JsonObject();
            //登录的用户名
            String userName_ting = SharedPreferencesUtilsTing.readLoginUserName(this);
            jo.addProperty("userName",userName_ting);
//            jo.addProperty("kfz","zxt");
            jo.addProperty("answer",validateName);
            OkhttpUtilsTing.isPOST("AndroidUser/SecuritysetAdd",false, jo, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {}

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    Gson gson = new Gson();
                    final loginResultBeanTing<SetSecurityBeanTing> r = gson.fromJson(res, loginResultBeanTing.class);
                    SetSecurityActivityTing.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if(r.code == 200){
                                new AestheticDialog.Builder(SetSecurityActivityTing.this, DialogStyle.EMOTION, DialogType.SUCCESS)
                                        .setTitle("SUCCESS")
                                        .setMessage("设置密保成功")
                                        .setDuration(2000)
                                        .show();
                                //保存密保到SharedPreferences
                                SharedPreferencesUtilsTing.saveSecurity(SetSecurityActivityTing.this,validateName);
                                //设置跳转时长
                                Timer timer=new Timer();
                                TimerTask task=new TimerTask() {
                                    @Override
                                    public void run() {
                                        SetSecurityActivityTing.this.finish();//关闭设置密保页面
                                    }
                                };
                                timer.schedule(task,2000);
                            }else {
                                new AestheticDialog.Builder(SetSecurityActivityTing.this, DialogStyle.EMOTION, DialogType.ERROR)
                                        .setTitle("ERROR")
                                        .setMessage(r.msg)
                                        .setDuration(1500)
                                        .show();
                            }
                        }
                    });
                }
            });
        }
    }

}

